from django.db import models


# Create your models here.

class Icon(models.Model):
    title = models.CharField(verbose_name='图标代码', max_length=32)
    tag = models.CharField(verbose_name='图标标签', max_length=512)

    def __str__(self):
        return self.title


class Menu(models.Model):
    title = models.CharField(verbose_name='菜单名称', max_length=32)
    icon = models.ForeignKey(verbose_name='图标', to=Icon, null=True, blank=True, on_delete=models.CASCADE)
    index = models.IntegerField(verbose_name='菜单序列', null=True, blank=True)

    def __str__(self):
        return self.title


class Permissions(models.Model):
    title = models.CharField(verbose_name='标题', max_length=32)
    url = models.CharField(verbose_name='含正则的URL', max_length=128)
    name = models.CharField(verbose_name='URL别名', max_length=32, unique=True)
    menu = models.ForeignKey(
        verbose_name='所属菜单',
        to=Menu, null=True, blank=True,
        help_text='null表示不是菜单，非null表示二级菜单',
        on_delete=models.CASCADE
    )
    pid = models.ForeignKey(verbose_name='关联的权限', to='Permissions', null=True, blank=True, related_name='parents',
                            help_text='对于非菜单权限需要选择一个可以成为菜单的权限，用户做默认展开和选中菜单', on_delete=models.CASCADE)

    def __str__(self):
        return self.title


class Role(models.Model):
    title = models.CharField(verbose_name='角色名称', max_length=16)
    permissions = models.ManyToManyField(verbose_name='所拥有的权限', to='Permissions', blank=True)

    def __str__(self):
        return self.title


class Department(models.Model):
    title = models.CharField(verbose_name='部门名称', max_length=32)

    def __str__(self):
        return self.title


class User(models.Model):
    username = models.CharField(verbose_name='用户账号', max_length=32)
    passwd = models.CharField(verbose_name='密码', max_length=256)
    fullname = models.CharField(verbose_name='姓名', max_length=32)
    email = models.EmailField(verbose_name='邮箱地址', max_length=32)
    phone = models.CharField(verbose_name='手机号码', max_length=16)
    roles = models.ManyToManyField(verbose_name='角色', to=Role, blank=True)
    depart = models.ForeignKey(verbose_name='部门', to='Department', on_delete=models.CASCADE, blank=True, null=True)

    def __str__(self):
        return self.fullname

    # class Meta:
    #     """
    #     开启此选项后，数据库迁移时，不再为此models类创建 表结构；
    #     此类可以做为父类，被其他model类继承
    #     """
    #     abstract = True
